#include <bits/stdc++.h>
using namespace std;
#define int long long
const int N = 10010;
int n, m, atmp[N], c[N], T, X[5], a[N], winners[N];
vector<int> wins[N];
bitset<N> G[N];
int query(int x)
{
    int k = ceil(log2(x)), r = pow(2, k), bian = 2;
//cout << k << " " << r << " " << bian << endl;
/*
    for (int i = 1; i <= r; i ++ ) winners[i] = i;
    for (int rnd = 1; rnd <= k; rnd ++ )
    {
        int xx = 1;
        for (int j = 1; j <= r; j += bian, xx ++ )
        {
            bool zhu = G[rnd][xx];
            int p1 = winners[j], p2 = winners[j + bian / 2];
            //cout << p1 << " vs " << p2 << ", winner: ";
            if (zhu == 0) {
                if (p1 > p2) swap(p1, p2);
            } else {
                if (p1 < p2) swap(p1, p2);
            }
            if ((p1 <= x ? a[p1] : 0) >= rnd) winners[j] = p1;//,cout<<p1;
            else winners[j] = p2;//,cout<<p2;
            //cout<<endl;
        }
        //for (int j = 1; j <= r; j += bian) cout << winners[j] << " ";
        //cout << endl;
        bian *= 2;
    }
    int id = winners[1];*/
    int id = 0;
    for (int i = 1; i <= r; i ++ ) wins[i].push_back(i);
    bian = 2;
    for (int rnd = 1; rnd <= k; rnd ++ )
    {
        int xx = 1;
        for (int j = 1; j <= r; j += bian, xx ++ )
        {
            bool zhu = G[rnd][xx];
            //cout<<rnd<<" "<<xx<<" "<<zhu<<endl;
            set<int> v;
            int ok = 0;
            for (int p1 : wins[j])
                for (int p2 : wins[j + bian / 2])
                {
                    if (zhu == 0) {
                        if (p1 > p2) swap(p1, p2);
                    } else {
                        if (p1 < p2) swap(p1, p2);
                    }
                    //cout << "p1, p2, zhu: " << p1 << " " << p2 << " " << zhu << " " <<rnd<<" "<<xx<<endl;
                    if (p1 <= x) {
                        if (a[p1] >= rnd) v.insert(p1);
                        else v.insert(p2);
                    }
                    else v.insert(p1), v.insert(p2);
                }
            wins[j].clear(), wins[j + bian / 2].clear();
            for (int iii : v) wins[j].push_back(iii);
            // if (wins[j].size() == 1) cout << wins[j][0] << endl;
            //else cout << wins[j][0]<<" "<<wins[j][1]<<endl;
            //int p1 = winners[j], p2 = wins[j + bian / 2];
            //cout << p1 << " vs " << p2 << ", winner: ";

            //if ((p1 <= x ? a[p1] : 0) >= rnd) winners[j] = p1;//,cout<<p1;
            //else winners[j] = p2;//,cout<<p2;
            //cout<<endl;
        }

        //cout<<"-------------------"<<endl;
        //cout << endl;
        bian *= 2;
    }
    for (int i : wins[1]) id += i;
    wins[1].clear();
    return id;
}
signed main() {
    freopen("arena.in","r",stdin);
    freopen("arena.out", "w", stdout);
    ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
    cin >> n >> m;
    for (int i = 1; i <= n; i ++ ) cin >> atmp[i];
    for (int i = 1; i <= m; i ++ ) cin >> c[i];
    int k = ceil(log2(n)), r = pow(2, k - 1);
    for (int i = 1; i <= k; i ++ ) {
        for (int j = 1; j <= r; j ++ ) {
            char c;
            cin >> c;
            //cout<<i<<", "<<j<<", "<,c<<
            G[i][j] = c - '0';
        }
        r /= 2;
    }
    //cout << "all read" << endl;
    cin >> T;
    while (T -- ) {
        for (int i = 0; i < 4; i ++ ) cin >> X[i];
        for (int i = 1; i <= n; i ++ ) a[i] = atmp[i] ^ X[i % 4];
        //cout << "real a: ";
        //for (int i = 1; i <= n; i ++ ) cout << a[i] << " "; cout << endl;
        int res = 0;
        //cout << "queries: " << endl;
        for (int i = 1; i <= m; i ++ )
        {
            int t = query(c[i]);
            //cout << t << " ";
            res ^= i * t;
        } //cout << endl << query(3) << endl;
        cout << res << "\n";
    }
    return 0;
}
